package com.jinzhi.job.quartz;

import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.dao.ArchivesDao;
import com.jinzhi.jzweb.dao.ExaminationCostDao;
import com.jinzhi.jzweb.domain.ArchivesDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author mengzhichao
 * @create 2022-11-30-9:02
 */
@Component // 把此类托管给 Spring，不能省略
public class TaskUtils {

    @Autowired
    private ExaminationCostDao examinationCostDao;

    @Autowired
    private ArchivesDao archivesDao;

    // 添加定时任务
//    @Scheduled(cron = "0/2 * * * * ?") // cron表达式：两秒执行一次
    @Scheduled(cron = "0 0 12 ? * WED") // cron表达式：表示每个星期三中午12点执行一次
    public void doTask(){
        List<Map<Object, Object>> list = examinationCostDao.selectSchoolName();
        if (list.size()>0){ //不为空
            List<ArchivesDO> insertList = new ArrayList<>();
            for (int i = 0;i<list.size();i++){
                ArchivesDO archivesDO = new ArchivesDO();
                archivesDO.setSchoolName(String.valueOf(list.get(i).get("schoolName")));
                archivesDO.setProvince(String.valueOf(list.get(i).get("province")));
                archivesDO.setUpdateTime(new Date());
                insertList.add(archivesDO);
            }
            for (int i =0;i<insertList.size();i++){
                archivesDao.insert(insertList.get(i));
                System.out.println("第"+(i+1)+"次执行新增定时任务！");
            }

        }

    }
}
